import socket
import os
import hashlib
import time


def send_information(cmd,filename): #send-recv
    #print(cmd,filename)
    if os.path.isfile(filename):
        print(filename)
        size = os.stat(filename).st_size
        # ---
        send_len = len(str(size).encode('utf-8'))
        agent.send(str(send_len).encode('utf-8'))
        print("发送的大小：", size)
        agent.recv(1024)
        agent.send(str(size).encode('utf-8'))  # 发送数据的长度
        agent.recv(1024)  # 1

        # --------------------------------
        sec_len = len((str(cmd).encode("utf-8")))
        agent.send(str(sec_len).encode("utf-8"))
        agent.recv(1024)
        agent.send(str(cmd).encode("utf-8"))  # 发送文件类型
        agent.recv(1024)  # 2

        # --------------------------------
        third_len = len(str(filename).encode("utf-8"))
        agent.send(str(third_len).encode("utf-8"))
        agent.recv(1024)
        agent.send(str(filename).encode("utf-8"))  # 发送filename
        agent.recv(1024)  # 接收3

        f = open(filename, "rb")
        for line in f:
            agent.send(line)
        f.close()
        agent.recv(1024)  # 4

agent = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #生成socket连接对象

ip_port = ("localhost",6969)#地址和端口

agent.connect(ip_port) #连接

print("[*]服务器已连接......")
Linux_list = []
Windows_list = []
Web_list = []
Pcap_list = []

while True:

    path = "Linux_log"      #Linux
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        if not Linux_list:
            Linux_list.append(file)
            cmd = "Linuxcmd"
            send_information(cmd, file)
        else:
            if file not in Linux_list:
                Linux_list.append(file)
                cmd = "Linuxcmd"
                send_information(cmd, file)

    path = "Windows_log"      #Windows_log
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        if not Windows_list:
            Windows_list.append(file)
            cmd = "Windowscmd"
            send_information(cmd, file)
        else:
            if file not in Windows_list:
                Windows_list.append(file)
                cmd = "Windowscmd"
                print(cmd)
                send_information(cmd, file)

    path = "Web_log"      #Web
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        if not Web_list:
            Web_list.append(file)
            cmd = "Webcmd"
            send_information(cmd, file)
        else:
            if file not in Web_list:
                Web_list.append(file)
                cmd = "Webcmd"
                send_information(cmd, file)

    path = "pcap_log"      #pcap_log
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        #print(file)
        if not Pcap_list:
            Pcap_list.append(file)
            cmd = "Pcapcmd"
            send_information(cmd, file)
            #print(cmd,file)
        else:
            if file not in Pcap_list:
                Pcap_list.append(file)
                cmd = "Pcapcmd"
                send_information(cmd, file)
                #print(cmd, file)



agent.close()